package interfaz2;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * This class is intented to be used wherever a Dialog is needed in Dust. It
 * adds a bit of functionality to the Dialog class of SWT.
 * 
 * The main characteristic is that it has a "result" attribute that will be
 * inherit by subclasses. The subclass must implement its logic and when it
 * gather the information needed to pass to the caller of the DustDialog it
 * should put it in that attribute. The method runDialog() will pop up the
 * dialog and returns that attribute. From the view of the caller it must do a
 * cast to the proper object it is waiting for.
 * 
 * As an example of use, check how AddSubscriptionDialog use DustDialog. 
 * 
 * @author ole
 */
public abstract class DustDialog extends Dialog {
	protected Shell shell;
	protected Object result = null;
	
	/*
	 * Public methods of class DustDialog.
	 */
	
	/**
	 * Superconstructor. It sets the shell attribute and the Layout of this
	 * Dialog (a GridLayout).
	 * 
	 * @param parent
	 * 		The parent shell of this new Dialog.
	 */
	public DustDialog(Shell parent) {
		super(parent);
		
		this.shell = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		this.shell.setLayout(new GridLayout());
	}
	
	/**
	 * TODO: Since the termination of the dialog is made via disposing the
	 * Shell where it was created, will this need to create new Shells for the
	 * DustDialogs? It seems so... Improve it?.
	 */
	/**
	 * Convenience function to call when you want the DustDialog to finish.
	 */
	public void doExit() {
		this.shell.dispose();
	}
	
	/**
	 * Method that runs the dialog and returns the result attribute. Note that
	 * if you extend this class you should add logic which will put in the
	 * result attribute whatever you want to get outside of it. In those places
	 * you will have to make a cast.
	 * 
	 * @return
	 * 		The attribute Object "result". Subclasses must fill this attribute
	 * 		in their logic.
	 */
	public Object runDialog() {
		Display display = shell.getDisplay();
		
		/**
		 * TODO: Do you feel like every dialog of Dust needs to be resized to a
		 * better width and height so it would show all its text? Or just
		 * because that small dialog would be much more nicer if it would be
		 * just a bit bigger?
		 * 
		 * Here you have something to play with to improve it. This pack() will
		 * cause every dialog to be resized to its preferred size. Coment it
		 * and go to every DustDialog and set in its constructor the size (with
		 * the setSize() method) you want for that dialog... then start de test
		 * and debug and realize why i didn't loose time with this problem :).
		 * 
		 * I also think that dialogs needs to change their size to be nicer...
		 * but I am also a shell lover so I loose my time with the important
		 * things for Dust, not the appearance (also I am a really bad GUI
		 * designer!!!).
		 */
		shell.pack();
		shell.open();
		shell.forceActive();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())					
				display.sleep();
		}
		
		return result;
	}
}
